package com.aaa.dao.impl;

import com.aaa.dao.BaseDao;
import com.aaa.dao.StaffDao;
import com.aaa.entity.Staff;
import org.apache.commons.lang3.StringUtils;

import java.util.List;
import java.util.Map;

public class StaffDaoImpl implements StaffDao {

    private BaseDao baseDao = BaseDao.getInstance();

    @Override
    public Staff login(String staffId, String password) {
        String sql = "select * from staff where staffId = ? and password = ?  and status = 1";

        Object[] params = {staffId, password};
        List<Staff> staffList = baseDao.query(sql, params, Staff.class);

        if (staffList != null && !staffList.isEmpty()) {
            return staffList.get(0);
        }

        return null;
    }

    @Override
    public int updateStaffByStaffId(Staff staff) {
        String sql = "UPDATE staff SET `staffName` = ?," +
                "phone = ?," +
                "`idCard` = ?,`address` = ?," +
                "`status` = ?," +
                "`roleId` = ?,`momo` = ?" +
                " WHERE `staffId` = ?";
        Object[] arr = { staff.getStaffName(), staff.getPhone()
                , staff.getIdCard(), staff.getAddress(), staff.getStatus(),
                staff.getRoleId(), staff.getMomo(), staff.getStaffId()};

        return baseDao.executeUpdate(sql, arr);
    }

    @Override
    public int addStaffByStaffId(Staff staff) {
        String sql = "insert into staff(staffId,password,phone,idCard,address,createdTime,status,roleId," +
                "momo,staffName)" +
                " values(?,?,?,?,?,?,?,?,?,?)";
        Object[] arr = {staff.getStaffId(),staff.getPassword(), staff.getPhone()
                , staff.getIdCard(), staff.getAddress(), staff.getCreatedTime(), staff.getStatus(),
                staff.getRoleId(), staff.getMomo(),staff.getStaffName()};
        return baseDao.executeUpdate(sql, arr);
    }

    @Override
    public List<Staff> findAllStaff(Integer pageNumber, Integer pageSize, String searchId, String searchName) {

        String sql = "select s.*,r.roleName roleName from staff s,role r where s.roleId = r.id ";
        if (StringUtils.isNotBlank(searchId)) {
            sql += " and staffId = " + searchId;
            System.out.println(sql);
        }
        if (StringUtils.isNotBlank(searchName)) {
            searchName = "%" + searchName + "%";
            sql += " and staffName like '" + searchName + "'";
        }
        sql += " limit ?,?";

        Object[] params = {pageNumber, pageSize};
        List<Staff> staff = baseDao.query(sql, params, Staff.class);
        System.out.println(staff);
        return staff;
    }

    @Override
    public int deleteStaffByStaffId(int staffId) {
        String sql = "delete from staff where staffId = ?";
        Object [] arr = {staffId};
        return baseDao.executeUpdate(sql,arr) ;
    }




    public List<Staff> getMaxStaffId(){
        String sql = "select max(staffId) as staffId from staff ";
        return baseDao.query(sql,null, Staff.class);
    }

    @Override
    public int getAllStaffInfoCount(String searchId, String searchName) {
        String sql = "select count(1) len from staff where 1 = 1";
        if (StringUtils.isNotBlank(searchId)) {
            sql += " and staffId = " + searchId;
        }
        if (StringUtils.isNotBlank(searchName)) {
            searchName = "%" + searchName + "%";
            sql += " and staffName like '" + searchName + "'";
        }
        List<Map<String, Object>> maps = baseDao.query(sql, null);
        if (maps != null && maps.size() > 0) {
            Map<String, Object> map = maps.get(0);
            Integer res = Integer.parseInt(map.get("len") + "");
            return res;
        }
        return 0;
    }

    @Override
    public int updatePassword(String password,int staffId) {
        String sql = "update staff set password = ? where staffId = ?";
        Object[] params = {password,staffId};
        return baseDao.executeUpdate(sql,params);
    }
}
